﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AppModule.InterProcessComm;
using AppModule.NamedPipes;
namespace NamePipeServer
{
    public partial class Form1 : Form
    {
        public static IChannelManager pPipeManager;
        public static TextBox ptxtRef;

        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            ptxtRef = this.textBox1;
            start();
        }

        private void menu_click( object sender, System.EventArgs e)
        {
            switch ( Convert.ToInt32( (sender as MenuItem).Tag) )
            {
                case 1:
                    this.Visible = !this.Visible;
            	    break;
                case 2:
                    start(); break;
                case 3:
                    stop(); break;

            }

        }
    
        private void start()
        {
            if (pPipeManager != null) return;
            pPipeManager = new PipeManager();
            pPipeManager.Initialize();
        }

        private void stop()
        {
            if (pPipeManager!=null)
            {
                pPipeManager.Stop();
                pPipeManager = null;
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            ContextMenu cm = new ContextMenu();
            notifyIcon1.Text = "数据监测程序";
            notifyIcon1.Visible = true;

            MenuItem it;
            it = new MenuItem("显示/隐藏窗口");
            it.Tag = 1;
            it.Click += new System.EventHandler( this.menu_click );
            cm.MenuItems.Add(it);
            it = new MenuItem("启动");
            it.Tag = 2;
            it.Click += new System.EventHandler(this.menu_click);
            cm.MenuItems.Add(it);

            it = new MenuItem("停止");
            it.Tag = 3;
            it.Click += new System.EventHandler(this.menu_click);
            cm.MenuItems.Add(it);

            //it = new MenuItem("");
            //it.Tag = 4;
            //cm.MenuItems.Add(it);
            notifyIcon1.ContextMenu = cm;
            
        }
    }
}
